/**
 * @author Ahammer
 * @date    2009 12
 * @update  2009 12 Method CurrROI AllROIs
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import main.IqmTools;
import main.Tank;

public class IqmROISegmentGUI  extends IqmOperatorJFrame
							    implements IqmOperatorGUIBuilder, ActionListener, AdjustmentListener {

	private  ParameterBlockJAI    pbJAI         = null;  //  @jve:decl-index=0:
	
	private  JRadioButtonMenuItem  buttCurrROI       = null;
	private  JRadioButtonMenuItem  buttAllROIs       = null;	
	private  JRadioButtonMenuItem  buttMethodHidden  = null;
	private  JToolBar              jToolBarMethod    = null;
	private  ButtonGroup           buttGroupMethod   = null;	
	private  JCheckBox 			   jCheckBoxBinarize = null;
	
	/**
	 * constructor
	 */
	public IqmROISegmentGUI() {

	}


	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pbJAI
	 *
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pbJAI = arg;		
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		if (buttCurrROI.isSelected())  pbJAI.setParameter("Method", 0);
		if (buttAllROIs.isSelected())  pbJAI.setParameter("Method", 1);
		if (jCheckBoxBinarize.isSelected())    pbJAI.setParameter("Binarize", 1);
		if (!jCheckBoxBinarize.isSelected())   pbJAI.setParameter("Binarize", 0);
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	
	/**
	 * This method sets the current parameter values
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmROISegmentGUI: Current image not defined");
	    	   return;
	       }
//	    int pixelSize = pi.getColorModel().getPixelSize();
//      int numBands  = pi.getNumBands(); 
//      String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
        //String type = IqmTools.getCurrImgTyp(pi);

		if (pbJAI.getIntParameter("Method") == 0) buttCurrROI.setSelected(true);
		if (pbJAI.getIntParameter("Method") == 1) buttAllROIs.setSelected(true);
	    if (pbJAI.getIntParameter("Binarize") == 0)   jCheckBoxBinarize.setSelected(false);
	    if (pbJAI.getIntParameter("Binarize") == 1)   jCheckBoxBinarize.setSelected(true);
	    
	}

	/**
	 * This method creates and shows the GUI
	 */
	@Override
	public void createIqmOperatorGUI() {
		//opGUIFrame = new ImgProcFuncJFrame();  //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame = new IqmROISegmentGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//this.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("ROISegment");
		//PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
		//int numBands = pi.getNumBands();
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height + 80);
		//this.setPreferredSize(new Dimension(800, 600));
		this.getJContentPane().add(getJToolBarMethod(),     getGridBagConstraintsButtonMethodGroup());		
		this.getJContentPane().add(getJCheckBoxBinarize(),  getGridBagConstraintsBinarize());

		this.pack();
		//this.repaint();
		if (pbJAI == null) pbJAI = new ParameterBlockJAI("IqmROISegment");  //nur wenn das erste mal aufgerufen
		//pbJAI = new ParameterBlockJAI("IqmROISegment"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pbJAI);
		setParameterValues();
	}

	//------------------------------------------------------------------------------
	private GridBagConstraints getGridBagConstraintsButtonMethodGroup(){
		GridBagConstraints gridBagConstraintsButtonMethodGroup = new GridBagConstraints();
		gridBagConstraintsButtonMethodGroup.gridx = 1;
		gridBagConstraintsButtonMethodGroup.gridy = 5;
		gridBagConstraintsButtonMethodGroup.gridwidth = 2;//?
		gridBagConstraintsButtonMethodGroup.insets = new Insets(20,0,0,0);  //top left bottom right		
		//gridBagConstraintsButtonMethodGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonMethodGroup;
	}
	private  GridBagConstraints getGridBagConstraintsBinarize(){
		GridBagConstraints gridBagConstraintsBinarize = new GridBagConstraints();
		gridBagConstraintsBinarize.gridx = 3;
		gridBagConstraintsBinarize.gridy = 6;
		gridBagConstraintsBinarize.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsBinarize.insets = new Insets(10,0,20,0);  //top, left bottom right
		//gridBagConstraintsBinarize.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsBinarize;
	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * This method updates the GUI if needed
	 * This method overrides IqmOperatorJFrame
	 */
	@Override
	public void updateIqmOperatorGUI(){
	
	}
	
	//Buttons--------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option: 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtCurrROI() {
		//if (buttCurrROI == null) {
			buttCurrROI  = new JRadioButtonMenuItem();
			buttCurrROI .setText("Current ROI");
			buttCurrROI .setToolTipText("only current ROI is segmented");
			buttCurrROI .addActionListener(this);
			buttCurrROI.setActionCommand("parameter");
		//}
		return buttCurrROI;
	}
	/**
	 * This method initializes the Option: 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtAllROIs() {
		//if (buttAllROIs == null) {
			buttAllROIs  = new JRadioButtonMenuItem();
			buttAllROIs .setText("All ROIs");
			buttAllROIs .setToolTipText("all ROIs are segmented");
			buttAllROIs .addActionListener(this);
			buttAllROIs.setActionCommand("parameter");
		//}
		return buttAllROIs;
	}

	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMethodHidden() {
		//if (buttMethodHidden == null) {
			buttMethodHidden  = new JRadioButtonMenuItem();
			buttMethodHidden .setText("Hidden");
			buttMethodHidden.setVisible(false);
		//}
		return buttMethodHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarMethod() {
//		if (jToolBarMethod== null) {
			jToolBarMethod = new JToolBar("Options");
			jToolBarMethod.setOrientation(0); // 0 Horizontal 1 Vertical orientation
			jToolBarMethod.add(getJRadioButtonMenuButtCurrROI());
			jToolBarMethod.add(getJRadioButtonMenuButtAllROIs());
			jToolBarMethod.add(getJRadioButtonMenuButtMethodHidden());
			//jToolBarMethod.addSeparator();
			this.setButtonGroupMethod();	//Gruppieren der JRadioButtons
//		}
		return jToolBarMethod;
	}
	private  void setButtonGroupMethod() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupMethod = new ButtonGroup();
			buttGroupMethod.add(buttCurrROI);
			buttGroupMethod.add(buttAllROIs);
			buttGroupMethod.add(buttMethodHidden);
	}
	//--------------------------------------------------------------------------------------------	

	/**
	 * This method initializes jCheckBoxBinarize	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxBinarize() {
		if (jCheckBoxBinarize == null) {
			jCheckBoxBinarize = new JCheckBox();
			jCheckBoxBinarize.setText("Binarize");
			jCheckBoxBinarize.addActionListener(this);			
			jCheckBoxBinarize.setActionCommand("parameter");			
		}
		return jCheckBoxBinarize;
	}

	
	//--------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmROISegmentGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmROISegment".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}

		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	@Override
	public void adjustmentValueChanged(AdjustmentEvent e) {
		Object obE = e.getSource();
		this.setParameterBlock();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmROISegmentGUI gui = new IqmROISegmentGUI();
				gui.createIqmOperatorGUI();

			}
		});
	}



	
}//END
